﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pavilion.Locks
{
    public class LockPredicateMonitor : LockMonitorBase
    {
        private Func<bool> predicate;
        private IPropertyChanged item;

        public LockPredicateMonitor(IPropertyChanged item, Func<bool> predicate)
        {
            this.item = item;
            this.predicate = predicate;
        }

        public override void Dispose()
        {
            item.PropertyChanged -= GameTool_PropertyChanged;
            predicate = null;
        }

        public override void Load(GameItem gameItem)
        {
            item.PropertyChanged += GameTool_PropertyChanged;

            UpdateLock();
        }

        public void UpdateLock() { IsOpen = predicate.Invoke(); }

        private void GameTool_PropertyChanged(object sender, string propertyName) { UpdateLock(); }
    }
}
